import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/view/record/items/data_left_list.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';
import '../../controller/record/transfer_logic.dart';
import '../../entity/record/football/zq_transfer_entity.dart';
import '../../resources/colours.dart';
import '../../resources/styles.dart';
import '../../utils/utils.dart';
import '../../widgets/base/qkd_text.dart';
import '../../widgets/loading/no_data_widget.dart';
import 'items/transfer_card.dart';

class ZqTransferPage extends StatefulWidget {
  const ZqTransferPage(this.leagueId, {Key? key, required this.tag})
      : super(key: key);

  final int leagueId;
  final String tag;

  @override
  State<ZqTransferPage> createState() => _ZqTransferPageState();
}

class _ZqTransferPageState extends State<ZqTransferPage>
    with AutomaticKeepAliveClientMixin {
  late TransferLogic logic;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<TransferLogic>(
      tag: widget.tag,
      initState: (state) {
        logic = Get.put(
          TransferLogic(widget.leagueId, widget.tag),
          tag: widget.tag
        );
      },
      builder: (_) {
        return VisibilityDetector(
          key: Key(widget.tag),
          onVisibilityChanged: (VisibilityInfo info) {
            logic.visible = !info.visibleBounds.isEmpty;
          },
          child: LoadingWidget<bool>(
            isLoading: logic.left == null,
            data: logic.left?.length == 0,
            child: () => Row(
              children: [
                DataLeftList(logic.left!, logic.structIndex, ((p0) {
                  if (p0 == 0) {
                    Utils.onEvent('sjpd_zh/jf',
                        params: {'sjpd_zh/jf': '0'});
                  } else if (p0 == 1) {
                    Utils.onEvent('sjpd_zh/jf',
                        params: {'sjpd_zh/jf': '1'});
                  }
                  logic.onSelectLeft(p0);
                })),
                right(),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget right() {
    return Expanded(
        child: Container(
          color: Colours.white,
          child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 8),
                child: Column(
          children: [
            const SizedBox(
              height: 38,
              child: DefaultTextStyle(
                style: TextStyle(color: Colours.grey66, fontSize: 13),
                child: Row(
                  children: [
                    SizedBox(
                      width: 34,
                      child: QkdText('球员', textAlign: TextAlign.center)
                    ),
                    Spacer(),
                    SizedBox(
                      width: 55,
                      child: QkdText('原球队', textAlign: TextAlign.center),
                    ),
                    SizedBox(width: 29),
                    SizedBox(
                      width: 55,
                      child: QkdText('新球队', textAlign: TextAlign.center),
                    )
                  ],
                ),
              ),
            ),
            logic.showTransfer!.length == 0
                ? const Expanded(child: NoDataWidget())
                : Expanded(
                    child: MediaQuery.removePadding(
                    context: context,
                    removeTop: true,
                    child: ListView.builder(
                      itemBuilder: (context, index) => cell(index),
                      itemCount: logic.showTransfer!.length,
                    ),
                  ))
          ],
                ),
              ),
        ));
  }

  Widget cell(int index) {
    Transfers transfer = logic.showTransfer![index];
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        Utils.onEvent('sjpd_zh/jf', params: {'sjpd_zh/jf': '2'});
        ZqTransferCard.show(transfer);
      },
      child: Container(
        height: 50,
        padding: const EdgeInsets.symmetric(vertical: 5),
        child: Row(children: [
          CachedNetworkImage(
            height: 32,
            width: 24,
            fit: BoxFit.cover,
            imageUrl: transfer.photo ?? '',
            placeholder: (context, url) => Styles.placeholderIcon(),
            errorWidget: (context, url, error) => Image.asset(
              Utils.getImgPath('zq_placeholder.png'),
              height: 32,
              width: 24,
            ),
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                QkdText(
                  (transfer.nameChs ?? transfer.nameEn) ?? '',
                  maxLines: 1,
                  fontSize: 13,
                ),
                const SizedBox(height: 2),
                QkdText(
                  transfer.transferTypeCn ?? '',
                  color: Colours.grey66, fontSize: 11,
                )
              ],
            ),
          ),
          SizedBox(
            width: 55,
            child: QkdText(
              transfer.fromTeamCn ?? '',
              maxLines: 2,
              textAlign: TextAlign.center,
              fontSize: 11,
            ),
          ),
          const SizedBox(width: 8),
          Image.asset(
            Utils.getImgPath('transfer_arrow.png'),
            width: 13,
            height: 11,
          ),
          const SizedBox(width: 8),
          SizedBox(
            width: 55,
            child: QkdText(
              transfer.toTeamCn ?? '',
              maxLines: 2,
              textAlign: TextAlign.center,
              fontSize: 11,
            ),
          )
        ]),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
